<template>
	<view class="content">
		<image v-for="(logoSrc,i) in logoSrcs" :key="i" class="logo" :src="logoSrc"></image>
		<view>
			<button @click="inter(1)">活体检测</button>
			<button @click="inter(2)">人脸图像采集</button>
			<button @click="inter(3)">摄像头权限</button>
		</view>
		<view>
			默认静音(仅Android有效)
			<switch :checked="!isSound" @change="(e)=>{isSound=!e.target.value}" />
		</view>
		<view style="margin-top: 20rpx;">
			bestImage0默认截取脸部区域
			<switch :checked="AutoClip" @change="(e)=>{AutoClip=e.target.value}" />
		</view>
		<view style="margin-top: 20rpx;">
			后置摄像头
			<switch :checked="backCamera" @change="(e)=>{backCamera=e.target.value}" />
		</view>
		<scroll-view scroll-y="true">
			<view class="msg" v-html="msg">
			</view>
		</scroll-view>
    <jyf-parser  :html="nodes" ref="article"></jyf-parser>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				msg: '',
				logoSrcs: ['/static/logo.png'],
				isSound: true,
				AutoClip: true,
				backCamera: false,
        nodes:'<p style="text-indent:28px;line-height:28px">China Eastern Airlines and other <span style="text-decoration:underline;">domestic</span> airlines have begun to allow mobile phone use on their flights or announced plans to cancel the ban(<span style=";font-family:宋体">禁令</span>).</p><p style="text-indent:28px;line-height:28px">On Thursday, China Eastern Airlines and Hainan Airlines began to allow passengers to use portable electronic devices( PED), such as smart phones and tablets, but phones still have to be changed to airplane mode(<span style=";font-family:宋体">飞行模式</span>). China Southern Airlines and Xiamen Airlines announced Thursday they would allow mobile phone use on all their flights soon.</p><p style="text-indent:21px;line-height:28px">The permission comes after the Civil. Aviation Administration of China(<span style=";font-family:宋体">中国民用航空局</span>) announced that the ban would be canceled earlier. According to the guideline before, staff in the planes needed to ask passengers to turn off any devices that could interrupt the flight, and even airplane mode was not permitted.</p><p style="text-indent:21px;line-height:28px">In-flight Wi-Fi is available on 74 of China Eastern Airlines" aircrafts, covering all international routes and 166 domestic routes. Hainan Airlines has enabled in-flight Wi-Fi on 17 of its airplanes.<span style="font-size:3px;color:white">[</span><span style="font-size:3px;font-family:宋体;color:white">来源</span><span style="font-size:3px;color:white">:</span><span style="font-size:3px;font-family:宋体;color:white">学科网</span><span style="font-size:3px;color:white">]</span></p><p style="text-indent:21px;line-height:28px"><span style=";font-family:宋体">“</span>Before I just slept aboard the airplane, sometimes read magazines in front of me and then had nothing to do. But now, I can use my social network accounts like Weibo or We-chat ,<span style=";font-family:宋体">”</span> said a passenger named Zuo, who flight with Wi-Fi from Shanghai Thursday</p><p style="text-indent:28px;line-height:28px">Before 2018, mobile phone use on aircraft was banned over safety concerns.The ban is a common international practice, but with technological development and a higher demand from passengers, the ban has been canceled by some airlines.</p><p style="text-indent:28px;line-height:28px">Other Chinese airlines are also considering allowing mobile phone use. A number of airlines from other countries already provide in-flight Wi-Fi service, an important factor for passengers choosing flights of different airlines.</p><p><br></p>'
			}
		},
		onLoad() {
			
		},
		methods: {
			inter(inx) {
				const _self = this;
				const PPFace = uni.requireNativePlugin('PP-BaiduFace');
				let config = {
					licenseName: 'idl-license',
					licenseSuffix: 'face-android',
					licenseId: 'facelive123-face-android',
					liveActionArray: ["Eye"],
					bgColor: "#56445D",
					textColor: "#C5E99B",
					isSound: _self.isSound, //默认是否开启语音提示（仅Android有效）默认值：false
					AutoClip: _self.AutoClip, //自动裁剪用户脸部区域 默认：false
					isBackCamera: _self.backCamera //启用后置摄像头
				}

				if (plus.os.name == 'iOS') {
					config.licenseSuffix = 'face-ios';
					config.licenseId = 'ppfaceios-face-ios';
				}

				if (inx == 1) {
					PPFace.faceliveness(config, result => {
						_self.msg = new Date().getHours() + ':' + new Date().getMinutes() + '  ' + JSON.stringify(result) + '<br/>' +
							_self.msg;
						if (result.FaceStatusEnum == '"OK"') {
							// console.log(result)
							_self.saveImgs(result.base64ImageMap);
						}
					});
					if (plus.os.name == 'Android')
						setTimeout(function() {
							PPFace.ckcamera({}, result => {
								console.log(result)
							});
						}, 100);
				} else if (inx == 2) {
					PPFace.recycler(config, result => {
						_self.msg = new Date().getHours() + ':' + new Date().getMinutes() + '  ' + JSON.stringify(result) + '<br/>' +
							_self.msg;
						if (result.FaceStatusEnum == '"OK"') {
							_self.saveImgs(result.base64ImageMap);
						}
					});
					if (plus.os.name == 'Android')
						setTimeout(function() {
							PPFace.ckcamera({}, result => {
								
							});
						}, 3000);
				} else {
					PPFace.ckcamera({}, result => {});
				}

			},
			saveImgs(imgsobj) {
				let _self = this;
				_self.logoSrcs = [];
				for (let key in imgsobj) {
					var base64data = imgsobj[key];
					_self.bitmapsave(base64data);
					let base = "data:image/png;base64,"+base64data
					this.postData(base)
				}
			},
			bitmapsave(basedata) {
				let _self = this;
				var bitmap = new plus.nativeObj.Bitmap("test");
				bitmap.loadBase64Data(basedata, function(e) {
					bitmap.save('_doc/_tmp/head' + new Date().getTime() + '.png', {}, function(e) {
						_self.logoSrcs.push(e.target);
					});
					
					// this.uploadFile(e.target)
				}, function() {
					console.log('加载Base64图片数据失败：' + JSON.stringify(e));
				});
			},
			async postData(base64Data) {
				try {
					let uploadData = {
						base64Data:base64Data
					}
					let data = await this.$wxhttp.post({
						url:'/api/afr/face/faceRecognize',
						data:uploadData,
					})
					if( data.rspBody.person ){
						uni.showToast({
							title:"欢迎" + data.rspBody.person.personName + "！",
							duration:2000
						})
					}else{
						uni.showToast({
							title:"您还没有录入人脸信息！",
							duration:2000
						})
					}
				} catch (e) {
					console.log(e)
				}
			},
			async uploadFile(filePath) {
				try {
					let data = await this.$wxhttp.uploadFile({
						url: '/api/afr/face/faceRecognize2',
						name: "file",
						filePath: filePath,
					})
					let obj = JSON.parse(data)
					console.log(data)
					if( obj.rspBody.person ){
						uni.showToast({
							title:"登录成功！欢迎" + obj.rspBody.person.personName + "！",
							duration:2000
						})
					}else{
						uni.showToast({
							title:"您还没有录入人脸信息！",
							duration:2000
						})
					}
				} catch (e) {
					console.log(e)
				}
				console.log(filePath)
			},
			
		}
	}
</script>

<style>
	.content {
		text-align: center;
		height: 400upx;
	}

	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 50upx;
	}

	.title {
		font-size: 36upx;
		color: #8f8f94;
	}

	.msg {
		text-align: left;
		padding: 40rpx;
		font-size: 25rpx;
		color: #F0AD4E;
	}
</style>
